import { app, BrowserWindow, IpcMainEvent, IpcMainInvokeEvent } from 'electron'
import { createWindow, OptionsType } from './createWindow'

export const config: Record<WindowsNameType, { id: number; option: OptionsType }> = {
  search: {
    id: 0,
    option: {
      hash: '',
      // openDevTool: true,
      initShow: true,
    },
  },
  code: {
    id: 0,
    option: {
      width: 1200,
      height: 750,
      hash: '/#config/category/contentList',
      // hash: '/#config',
      frame: true,
      transparent: false,
    },
  },
  config: {
    id: 0,
    option: {
      width: 600,
      height: 400,
      hash: '/#config',
      frame: true,
      transparent: false,
    },
  },
}
export const getByNameWindow = (name: WindowsNameType) => {
  let win = BrowserWindow.fromId(config[name].id)
  if (!win) {
    win = createWindow(config[name].option)
    config[name].id = win.id
  }
  return win
}
export function getWindowByEvent(event: IpcMainEvent | IpcMainInvokeEvent) {
  return BrowserWindow.fromWebContents(event.sender)!
}
app.whenReady().then(() => {
  getByNameWindow('search')
})
